<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
error_reporting(E_ALL);
include_once('controller/includes/db_access.php');

function dateformat($param) {
    // format "2012-06-19"  
    $dt = strftime("%Y-%m-%d", strtotime($param));
    // convertir les accents (pour encodage UTF-8)  
    $dt = mb_convert_encoding($dt, 'utf-8');
    return $dt;
}

//1 post datum from stap1
$van_datum = dateformat($_POST['van']);
$tot_datum = dateformat($_POST['tot']);
//$set_id = $_POST['set_id'];


//1.1 count aantal dagen
function daysDifference($endDate, $beginDate) {
    //explode the date by "-" and storing to array
    $date_parts1 = explode("-", $beginDate);
    $date_parts2 = explode("-", $endDate);
    //gregoriantojd() Converts a Gregorian date to Julian Day Count
    $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
    $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
    return $end_date - $start_date;
}

$total_day = daysDifference($tot_datum, $van_datum);

  

$prodId_totalPerProdInSet= array();
    $SQL_totalPerProdInSet = "SELECT * FROM set2product WHERE set_id=".$_POST['set_id'];
    $query_totalPerProdInSet = mysql_query($SQL_totalPerProdInSet);
    while ($row_totalPerProdInSet = mysql_fetch_object($query_totalPerProdInSet)){
    $totalPerProdInSet = $row_totalPerProdInSet->aantal;
    
    $prodId_totalPerProdInSet[$row_totalPerProdInSet->product_id]=$totalPerProdInSet;
    
    //print_r($prodId_totalPerProdInSet);
   
    
    }
    
    
// STAP 2


$prodcten_beschikbaarheid=array();

$SQL_all_products = "SELECT * FROM product";
$query_all_products = mysql_query($SQL_all_products);
 while ($row_all_products = mysql_fetch_object($query_all_products)) {
     
     $pr_id=$row_all_products->product_id;
     $aantal_voorraad=$row_all_products->aantal;
   
     $begin_dag = $van_datum;  
     
     for ($i = 1; $i <= $total_day; $i++) {
         
        // CHECK beschikbaarheidstabel 
        $sql_inhuur = "SELECT SUM(aantal) AS totaal,product_id FROM beschikbaarheid WHERE datum='$begin_dag' AND product_id=" . $pr_id;
        $result_inhuur = mysql_query($sql_inhuur);
        $rows_inhuur = mysql_fetch_object($result_inhuur);
        
        // bereken hoeveel beschikbaar
        //echo $begin_dag.' | '.$pr_id.' | '.
        $beschikbaar_aantal =$aantal_voorraad-($rows_inhuur->totaal+$prodId_totalPerProdInSet[$pr_id]);
        
        if (!array_key_exists($pr_id,$prodcten_beschikbaarheid)) {
            
           $prodcten_beschikbaarheid[$pr_id]=$beschikbaar_aantal;
           //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S1<br />';
        } else {
            
            if ($beschikbaar_aantal<$prodcten_beschikbaarheid[$pr_id]) {
                $prodcten_beschikbaarheid[$pr_id]=$beschikbaar_aantal;
               // echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S2<br />';
            } else {
                //echo $begin_dag.' | '.$pr_id.' | '.$beschikbaar_aantal .' | S3<br />';
            }
        }
         
         $date = strtotime(date("Y-m-d", strtotime($begin_dag)) . " +1 day");
         $begin_dag = date('Y-m-d', $date);
        
        
     }
     
  
 }
 //print_r($prodcten_beschikbaarheid);
 
 
 // PAGE CONTENT
 ?>
<form name="form1" action="stap4.php" method="post">  
 <input type="hidden" name="van" value="<?php echo $_POST['van']; ?>" />
 <input type="hidden" name="tot" value="<?php echo $_POST['tot']; ?>" />
 <input type="hidden" name="set_id" value="<?php echo $_POST['set_id']; ?>" />
   <input type="hidden" name="dt" value="<?php echo $_POST['dt']; ?>" />
    <input type="hidden" name="aantal" value="<?php echo $_POST['aantal']; ?>" />
<table border="0" cellspacing="5" cellpadding="5">
    <thead>
        <tr>
            <th colspan="3"><h1>stap3 losse appartuur/DJ's/Bediening / Extra's</h1></th>

</tr>
</thead>
<tbody>
 
 <?php
$select_allecategorie = mysql_query("SELECT * FROM categorieen C, product P WHERE C.categorie_id = P.categorie_id GROUP BY C.categorie_id");

while ($rows_allecategorie = mysql_fetch_object($select_allecategorie)) {
            
$categorie_id = $rows_allecategorie->categorie_id;

    ?> 
 <tr>
<td colspan="2" ><?php echo $rows_allecategorie->categorie; ?></td>
<td></td>
</tr>
                                    <?php
 $SQL_alleproducts = "SELECT * FROM product WHERE categorie_id=".$categorie_id." ORDER BY  huurprijs ASC";
$query_alleproducts = mysql_query($SQL_alleproducts);
 while ($row_alleproducts = mysql_fetch_object($query_alleproducts)) {

 ?>    
    
<tr>
          <td colspan="2"><select name="product<?php echo $row_alleproducts->product_id ?>" <?php echo  (($prodcten_beschikbaarheid[$row_alleproducts->product_id]<=0 )&& ($total_day != 0)) ? 'disabled="disabled"' : ''; ?> >
                    <option>--aantal--</option>
                    
                    <?php if(($prodcten_beschikbaarheid[$row_alleproducts->product_id]) > 0 ){ 
                        $aantal = $prodcten_beschikbaarheid[$row_alleproducts->product_id];              
                        }
                        else
                            {
                            $aantal=$row_alleproducts->aantal;
                        
                        } ?>

                    <?php for ($i = 1; $i <= $aantal; $i++) { ?>
                        <option value="<?php echo $i ; ?>" ><?php echo $i; ?></option>
                    <?php } ?>
                </select>
         <?php echo $row_alleproducts->naam; ?>
                <?php if ((($prodcten_beschikbaarheid[$row_alleproducts->product_id])<=0 )&& ($total_day != 0))
                    echo '<font color="red">Niet beschikbaar</font>'; ?>
          </td>
            <td> <?php if((($prodcten_beschikbaarheid[$row_alleproducts->product_id])<=0 ) && ($total_day != 0)) 
                    echo '<font color="#ccc"> Huurprijs: &euro;'.$row_alleproduct->huurprijs.'</font> uurprijs: &euro;'.$row_alleproducts->huurprijs; ?> </td>
        </tr>
     
 <?php    
 }
}
 
?>
 <tr>
        <td></td>
        <td><input type="submit" value="volgende stap"/></td>
        <td></td>
    </tr>
</tbody>
</table></form> 